import Cookies from 'js-cookie'
import useUserStore from '@/store/modules/user'

const useAppStore = defineStore(
  'app',
  {
    state: () => ({
      sidebar: {
        opened: false,  // 始终收缩
        withoutAnimation: false,
        hide: true      // 默认隐藏
      },
      device: 'desktop',
      size: Cookies.get('size') || 'default'
    }),
    actions: {
      toggleSideBar(withoutAnimation) {
        // 不需要让侧边栏展开，保持收缩
        this.sidebar.opened = !this.sidebar.opened;  // 切换状态
      },
      closeSideBar({ withoutAnimation }) {
        this.sidebar.opened = false;
        this.sidebar.withoutAnimation = withoutAnimation;
      },
      toggleDevice(device) {
        this.device = device;
      },
      setSize(size) {
        this.size = size;
        Cookies.set('size', size)
      },
      toggleSideBarHide(status) {
        this.sidebar.hide = status;  // 控制是否隐藏
      },
      setSidebarVisibilityBasedOnRole() {
        const userStore = useUserStore();
        // 假设用户角色存储在userStore.roles中
        if (userStore.roles.includes('admin') || userStore.roles.includes('agent')) {
          this.sidebar.hide = false; // 如果是管理员，则显示侧边栏
        } else {
          this.sidebar.hide = true;  // 如果不是管理员，则隐藏侧边栏
        }
      }
    }
  }
)

export default useAppStore;
